package view;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.LoginException;

import controller.Cashier;

/**
 * Servlet implementation class Logoff
 */
public class Logoff extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private Cashier cashier = null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Logoff() {
        super();
        cashier = Cashier.getInstance();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String forward = "";
		String logoffStatus = "";
		try {
			cashier.doLogoff((String)request.getSession().getAttribute("login"), (String)request.getSession().getAttribute("password"));
			forward = "/Index.jsp";
		} catch (LoginException e) {
			forward = "/Controller.jsp";
			logoffStatus = "Erro : sessão já foi encerrada...";
		}
		
		request.setAttribute("logoffStatus", logoffStatus);
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(forward);
		if (dispatcher != null) {
			dispatcher.forward(request, response);
		}
	}

}
